<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">医生名字</label>
        <el-input
          v-model="query.nickName"
          clearable
          placeholder="医生名字"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">排班日期</label>
        <date-range-picker v-model="query.scheduleDate" class="date-item" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="700px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small">
          <!--          <el-form-item label="医生ID" prop="doctorId">-->
          <!--            <el-input v-model="form.doctorId" style="width: 370px;" />-->
          <!--          </el-form-item>-->
          <el-form-item label="医生" prop="doctorId">
            <el-select
              v-model="form.doctorDatas"
              filterable
              style="width: 437px"
              multiple
              placeholder="请选择"
              default-first-option
              @remove-tag="deleteDoctor"
              @change="changeDoctor"
            >
              <el-option
                v-for="item in doctors"
                :key="item.professionalTitle"
                filterable
                :label="item.nickName"
                :disabled="item.isScheduled"
                :value="item.doctorId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="排班周几" prop="scheduleDate">
            <el-select
              v-model="form.scheduleDate"
              placeholder="请选择周几值班"
              multiple
              @change="changeDate()"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否上午值班">
            <el-switch
              v-model="form.isMorning"
              active-color="#13ce66"
              inactive-color="#ff4949"
            />
          </el-form-item>
          <el-form-item v-if="form.isMorning" label="上午值班时间">
            <el-time-select
              v-model="form.morningStartTime"
              :picker-options="{
                start: '06:00',
                step: '00:30',
                end: '12:00'
              }"
              placeholder="上午开始小时"
            />
            <el-time-select
              v-model="form.morningEndTime"
              :picker-options="{
                start: '6:00',
                step: '00:30',
                end: '12:00'
              }"
              placeholder="上午结束小时"
            />
          </el-form-item>
          <el-form-item label="是否下午值班">
            <el-switch
              v-model="form.isAfternoon"
              active-color="#13ce66"
              inactive-color="#ff4949"
            />
          </el-form-item>
          <el-form-item v-if="form.isAfternoon" label="下午值班时间">
            <el-time-select
              v-model="form.afternoonStartTime"
              :picker-options="{
                start: '12:00',
                step: '00:30',
                end: '18:00'
              }"
              placeholder="下午开始小时"
            />
            <el-time-select
              v-model="form.afternoonEndTime"
              arrow-control
              :picker-options="{
                start: '12:00',
                step: '00:30',
                end: '18:00'
              }"
              placeholder="下午结束小时"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="scheduleId" label="排班ID" />
        <el-table-column prop="nickName" label="医生" />
        <el-table-column prop="scheduleDate" label="排班日期" />
        <el-table-column prop="isMorning" label="值班时间">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isMorning == 1">上午</el-tag>
            <el-tag v-if="scope.row.isAfternoon == 1">下午</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="morningStartTime" label="上午开始小时" />
        <el-table-column prop="morningEndTime" label="上午结束小时" />
        <el-table-column prop="afternoonStartTime" label="下午开始小时" />
        <el-table-column prop="afternoonEndTime" label="下午结束小时" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column
          v-if="checkPer(['admin','doctorSchedule:edit','doctorSchedule:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDoctorSchedule from '@/api/system/doctorSchedule'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getAllDoctor } from '@/api/system/sysDoctor'
import DateRangePicker from '@/components/DateRangePicker/index.vue'

let userDoctor = []
const defaultForm = {
  scheduleId: null,
  doctorId: null,
  isAllDay: null,
  morningStartTime: null,
  morningEndTime: null,
  afternoonStartTime: null,
  afternoonEndTime: null,
  intervalMinutes: null,
  createTime: null,
  updateTime: null,
  isAfternoon: false,
  isMorning: false
}
export default {
  name: 'DoctorSchedule',
  components: { DateRangePicker, pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '排班',
      url: 'api/doctorSchedule',
      idField: 'scheduleId',
      sort: 'scheduleId,desc',
      crudMethod: { ...crudDoctorSchedule }
    })
  },
  data() {
    return {
      weekdays: [],
      options: [{
        value: 'Monday',
        label: '周一'
      }, {
        value: 'Tuesday',
        label: '周二'
      }, {
        value: 'Wednesday',
        label: '周三'
      }, {
        value: 'Thursday',
        label: '周四'
      }, {
        value: 'Friday',
        label: '周五'
      }, {
        value: 'Saturday',
        label: '周六'
      }, {
        value: 'Sunday',
        label: '周日'
      }
      ],
      doctor: [],
      doctors: [],
      // 多谢的时候使用
      doctorDatas: [],
      permission: {
        add: ['admin', 'doctorSchedule:add'],
        edit: ['admin', 'doctorSchedule:edit'],
        del: ['admin', 'doctorSchedule:del']
      },
      rules: {
        isAllDay: [
          { required: true, message: '是否全天（1: 是, 0: 否）不能为空', trigger: 'blur' }
        ],
        intervalMinutes: [
          { required: true, message: '排班时间间隔（分钟）不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'dayOfWeek', display_name: '星期几（MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY）' },
        { key: 'isAllDay', display_name: '是否全天（1: 是, 0: 否）' }
      ]
    }
  },
  methods: {
    printScope(row) {
      console.log(row)
    },
    // 新增前将多选的值设置为空
    [CRUD.HOOK.beforeToAdd]() {
      this.doctors = []
      this.doctor = []
      this.doctorDatas = []
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      console.log(crud)
      // form = null
      // this.form.doctorDatas = []
      // this.form.doctorDatas.push(crud.data.doctorId)
      this.getDoctors()
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.getDoctors()
      return true
    },
    // 获取弹窗里面的医生数据
    getDoctors() {
      getAllDoctor().then(res => {
        this.doctors = res
      }).catch(() => {
      })
    },
    // 选择医生
    changeDoctor(value) {
      userDoctor = []
      value.forEach(function(data, index) {
        console.log(data)
        const doctor = { id: data }
        defaultForm.doctorId = data
        userDoctor.push(doctor)
      })
    },
    // 删除医生
    deleteDoctor(value) {
      userDoctor.forEach(function(data, index) {
        if (data.id === value) {
          userDoctor.splice(index, value)
        }
      })
    },
    changeDate() {
      // this.form.scheduleDate = this.weekdays
    }
  }
}
</script>

<style scoped>

</style>
